<template>
  <el-dialog
    :title="!dataForm.pid ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户" prop="memberid">
      <el-input v-model="dataForm.memberid" placeholder="用户"></el-input>
    </el-form-item>
    <el-form-item label="项目" prop="coachid">
      <el-input v-model="dataForm.coachid" placeholder="项目"></el-input>
    </el-form-item>
    <el-form-item label="设备" prop="subjectid">
      <el-input v-model="dataForm.subjectid" placeholder="设备"></el-input>
    </el-form-item>
    <el-form-item label="数量" prop="count">
      <el-input v-model="dataForm.count" placeholder="数量"></el-input>
    </el-form-item>
    <el-form-item label="价格" prop="countprice">
      <el-input v-model="dataForm.countprice" placeholder="价格"></el-input>
    </el-form-item>
    <el-form-item label="真实价格" prop="realprice">
      <el-input v-model="dataForm.realprice" placeholder="真实价格"></el-input>
    </el-form-item>
    <el-form-item label="时间" prop="date">
      <el-input v-model="dataForm.date" placeholder="时间"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="state">
      <el-input v-model="dataForm.state" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="管理员" prop="admin">
      <el-input v-model="dataForm.admin" placeholder="管理员"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          pid: 0,
          memberid: '',
          coachid: '',
          subjectid: '',
          count: '',
          countprice: '',
          realprice: '',
          date: '',
          state: '',
          remark: '',
          admin: ''
        },
        dataRule: {
          memberid: [
            { required: true, message: '用户不能为空', trigger: 'blur' }
          ],
          coachid: [
            { required: true, message: '项目不能为空', trigger: 'blur' }
          ],
          subjectid: [
            { required: true, message: '设备不能为空', trigger: 'blur' }
          ],
          count: [
            { required: true, message: '数量不能为空', trigger: 'blur' }
          ],
          countprice: [
            { required: true, message: '价格不能为空', trigger: 'blur' }
          ],
          realprice: [
            { required: true, message: '真实价格不能为空', trigger: 'blur' }
          ],
          date: [
            { required: true, message: '时间不能为空', trigger: 'blur' }
          ],
          state: [
            { required: true, message: '状态不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          admin: [
            { required: true, message: '管理员不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.pid = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.pid) {
            this.$http({
              url: this.$http.adornUrl(`/jsf/privatecoachinfo/info/${this.dataForm.pid}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.memberid = data.privatecoachinfo.memberid
                this.dataForm.coachid = data.privatecoachinfo.coachid
                this.dataForm.subjectid = data.privatecoachinfo.subjectid
                this.dataForm.count = data.privatecoachinfo.count
                this.dataForm.countprice = data.privatecoachinfo.countprice
                this.dataForm.realprice = data.privatecoachinfo.realprice
                this.dataForm.date = data.privatecoachinfo.date
                this.dataForm.state = data.privatecoachinfo.state
                this.dataForm.remark = data.privatecoachinfo.remark
                this.dataForm.admin = data.privatecoachinfo.admin
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/jsf/privatecoachinfo/${!this.dataForm.pid ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'pid': this.dataForm.pid || undefined,
                'memberid': this.dataForm.memberid,
                'coachid': this.dataForm.coachid,
                'subjectid': this.dataForm.subjectid,
                'count': this.dataForm.count,
                'countprice': this.dataForm.countprice,
                'realprice': this.dataForm.realprice,
                'date': this.dataForm.date,
                'state': this.dataForm.state,
                'remark': this.dataForm.remark,
                'admin': this.dataForm.admin
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
